package com.demo.amusic;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.demo.amusic.dao.MusicDao;
import com.demo.amusic.entity.Music;
import com.google.gson.Gson;

public class EditActivity extends AppCompatActivity implements View.OnClickListener {
    Music music = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setStatusBarColor(this,R.color.titleColor);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        String data = getIntent().getStringExtra("data");
        if (data.isEmpty()){
            Toast.makeText(this,"信息有误",Toast.LENGTH_SHORT).show();
            finish();
        }else if (data.equals("new")){
            music = new Music();
            music.setName("");
            music.setAuthor("");
            music.setUrl("");
            music.setLocalUrl("");
            init();
        }else{
            music = new Gson().fromJson(data,Music.class);
            if (music == null ){
                Toast.makeText(this,"信息有误",Toast.LENGTH_SHORT).show();
                finish();
            }
            init();
        }


    }

    public static void setStatusBarColor(Activity activity, int colorId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(activity.getResources().getColor(colorId));
        }
    }

    private EditText name,author,url,localurl,type;
    Button save,back,delete,download;
    private void init() {
        back = findViewById(R.id.edit_back);
        save = findViewById(R.id.edit_save);
        delete = findViewById(R.id.edit_del);
        download = findViewById(R.id.edit_download);
        delete.setOnClickListener(this);
        download.setOnClickListener(this);
        back.setOnClickListener(this);
        save.setOnClickListener(this);
        name = findViewById(R.id.edit_name);
        author = findViewById(R.id.edit_author);
        url = findViewById(R.id.edit_url);
        localurl = findViewById(R.id.edit_localurl);
        type = findViewById(R.id.edit_type);
        name.setText(music.getName().isEmpty() ? "":music.getName());
        author.setText(music.getAuthor().isEmpty() ? "":music.getAuthor());
        url.setText(music.getUrl().isEmpty() ? "":music.getUrl());
        localurl.setText(music.getLocalUrl().isEmpty() ? "":music.getLocalUrl());
        type.setText(music.getType()+"");


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.edit_back:
                finish();
                break;
            case R.id.edit_save:
                save();
                finish();
                break;
            case R.id.edit_del:
                delete();
                break;
            case R.id.edit_download:
                download();
                break;
        }
    }

    private void download() {
        Toast.makeText(this,"下载即将开始...",Toast.LENGTH_SHORT).show();
    }

    private void delete() {
        if (music.getId()>0)
            MusicDao.getInstance(this).delete(music.getId());
        finish();

    }

    private void save() {  // http://music.163.com/song/media/outer/url?id=ID数字.mp3
        music.setName(name.getText().toString());
        music.setAuthor(author.getText().toString());
        music.setUrl(url.getText().toString());
        music.setLocalUrl(localurl.getText().toString());
        music.setType(Integer.parseInt(type.getText().toString()));
        long i = MusicDao.getInstance(this).addOrUpdata(music);
        Log.e("添加歌曲",music.toString());
    }
}